package cs2340.g21.wms;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
 * This is the class to represent users of the app.
 * @author Alexander
 * @version 1.0
 */
public class User implements Serializable {
    private String email;
    private String password;
    private String phoneNum;
    private List<Item> items;
    private boolean isAdmin;
    private boolean isLocked;
    private int id;
    private int loginAttempts;
    private List<Note> notes;
     
    public User(){
        this.email = "empty";
        this.password = "password";
        this.phoneNum = "Not provided";
        this.items = new ArrayList<Item>();
        this.isAdmin = false;
        this.isLocked = false;
        this.id = -1;
        this.notes = new ArrayList<Note>();
        this.loginAttempts = 0;
    }
    
    public User(String email, String password, int id){
        this();
        this.email = email;
        this.password = password;
        this.id = id;
    }
    
    /**
     * Checks password against the password 
     * stored for this account
     * @param password to check against this user's password
     */
    public boolean checkPass(String password){
        loginAttempts++;
        if(loginAttempts >= 3){
            isLocked = true;
            return false;
        }
        return this.password.equals(password);
    }
    
    /**
     * Adds an item to this user's list of items
     * @param itemId id of item to be added
     * @return true if add successful, false otherwise
     */
    public boolean addItem(Item item){
        return items.add(item);
    }
    
    /**
     * @param item to remove
     * @return true if item is removed, false otherwise
     */
    public boolean removeItem(Item item){
        return items.remove(item);
    }
    
    /**
     * @param note to add
     * @return true if note is added, false otherwise
     */
    public boolean addNote(Note note){
        return notes.add(note);
    }
    
    /**
     * @param note to remove
     * @return true if note is removed, false otherwise
     */
    public boolean removeNote(Note note){
        return notes.remove(note);
    }
    
    /**
     * @return this user's list of notes
     */
    public List<Note> getNotes(){
        return notes;
    }

    /**
     * @return this user's email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email to set as this user's email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return this user's password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password to set this user's pass to
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return this user's phone number
     */
    public String getPhoneNum() {
        return phoneNum;
    }

    /**
     * @param phoneNum to set this user's phoneNum to
     */
    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    /**
     * @return this user's list of items
     */
    public List<Item> getItems() {
        return items;
    }
    
    /**
     * resets the number of login pages
     */
    public void resetLogin(){
        loginAttempts = 0;
    }

    /**
     * @param items set this user's list of items
     */
    public void setItems(List<Item> items) {
        this.items = items;
    }

    /**
     * @return true if user is admin, false otherwise
     */
    public boolean isAdmin() {
        return isAdmin;
    }

    /**
     * @param isAdmin whether or not this user is an admin
     */
    public void setAdmin(boolean isAdmin) {
        this.isAdmin = isAdmin;
    }

    /**
     * @return true if user is locked, false otherwise
     */
    public boolean isLocked() {
        return isLocked;
    }
    
    /**
     * resets the login attempts and unlocks this user
     */
    public void unlock(){
        resetLogin();
        isLocked = false;
    }

    /**
     * @return this user's id
     */
    public int getId() {
        return id;
    }
    
    /**
     * Used when serializing User. Writes the instance's information to an output stream
     * so it can be passed from activity to activity
     */
    private void writeObject(java.io.ObjectOutputStream out) throws IOException{
        out.defaultWriteObject();   
    }

    /**
     * Used to read a user object from inputstream
     */
    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException{
        in.defaultReadObject();
    }
    
    /**
     * @return number of login attempts left
     */
    public int getAttempts(){
        return 3 - loginAttempts;
    }
}
